﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TinyMoocSite.Filters;
using TinyMoocSite.ViewModels;
using TinyMoocSite.Helpers;
using System.IO;

namespace TinyMoocSite.Controllers
{
    [Authorize]
    [HeaderFooterFilter]
    public class StudentController : Controller
    {
        public ActionResult Index(string Type = "Current")
        {
            CourseInfoHelper helper = new CourseInfoHelper();
            CourseList courseList = helper.GetCourseList(User.Identity.Name);
            if (Type == "Current")
            {
                courseList.CourseItems.RemoveAll(p =>
                    DateTime.Parse(p.StartTime) > DateTime.Now ||
                    DateTime.Parse(p.EndTime) <= DateTime.Now || 
                    p.Picked == false);
                ViewData["Current"] = "active";
            }
            else if (Type == "Opening")
            {
                courseList.CourseItems.RemoveAll(p =>
                    DateTime.Parse(p.EndTime) <= DateTime.Now);
                ViewData["Opening"] = "active";
            }
            else
            {
                courseList.CourseItems.RemoveAll(p =>
                    DateTime.Parse(p.EndTime) > DateTime.Now);
                ViewData["Previous"] = "active";
            }
            return View(courseList);
        }
        
        public ActionResult Resource(string Id)
        {
            CourseInfoHelper helper = new CourseInfoHelper();
            ResourceVM vm = helper.GetResourceData(Id);
            if (vm == null)
                return RedirectToAction("Index");
            return View(vm);
        }

        public ActionResult DownloadResource(string Id)
        {
            CourseInfoHelper helper = new CourseInfoHelper();
            string fileName = helper.DownloadResource(Id);
            FileStream fs = new FileStream(fileName, FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();

            Response.Charset = "UTF-8";
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            Response.ContentType = "application/octet-stream";

            Response.AddHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(fileName));
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
            return new EmptyResult();
        }
        
        public ActionResult AttachedPage(string Id)
        {
            CourseInfoHelper helper = new CourseInfoHelper();
            AttachedPageArray attachedPageArray = helper.GetAttachedPageArray(Id);
            if (attachedPageArray == null)
                return RedirectToAction("/Index");
            return View(attachedPageArray);
        }
        
        public ActionResult AttachedPageContent(string Id)
        {
            CourseInfoHelper helper = new CourseInfoHelper();
            AttachedPageContentVM vm = helper.GetAttachedPageContent(Id);
            if (vm == null)
                return RedirectToAction("/");

            return View(vm);
        }
        
        public ActionResult Discuss(string Id)
        {
            CourseInfoHelper helper = new CourseInfoHelper();
            DiscussVM discuss = helper.GetDiscussShortItemArray(Id);
            if (discuss == null)
                return RedirectToAction("/Index");
            return View(discuss);
        }

        public ActionResult AddDiscuss(string CourseId)
        {
            CourseInfoHelper helper = new CourseInfoHelper();
            AddDiscussForm form = helper.InitDiscussForm(CourseId);
            if (form == null)
                return RedirectToAction("/Index");
            return View(form);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult DoAddDiscuss(AddDiscussForm form)
        {
            CourseInfoHelper helper = new CourseInfoHelper();
            helper.AddDiscuss(form, User.Identity.Name);
            return RedirectToRoute(new
            {
                controller = "Student",
                action = "Discuss",
                Id = form.Id.ToString()
            });
        }

        public ActionResult DiscussContent(string CourseId, string DiscussId)
        {
            CourseInfoHelper helper = new CourseInfoHelper();
            DiscussContentVM vm = helper.GetDiscussContent(CourseId, DiscussId);
            if (vm == null)
                return RedirectToAction("/Index");
            return View(vm);
        }

        public ActionResult ReplyDiscuss(string CourseId, string DiscussId)
        {
            CourseInfoHelper helper = new CourseInfoHelper();
            ReplyDiscussForm vm = helper.InitReplyDiscussForm(CourseId, DiscussId);
            if (vm == null)
                return RedirectToAction("/Index");
            return View(vm);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult DoReplyDiscuss(ReplyDiscussForm form)
        {
            CourseInfoHelper helper = new CourseInfoHelper();
            helper.ReplyDiscuss(form, User.Identity.Name);
            return RedirectToRoute(new
            {
                controller = "Student",
                action = "DiscussContent",
                CourseId = form.Id.ToString(),
                DiscussId = form.PostId
            });
        }

        public ActionResult RegisterCourse(string Id)
        {
            CourseInfoHelper helper = new CourseInfoHelper();
            helper.RegisterCourse(Id, User.Identity.Name);
            return RedirectToRoute(new
            {
                controller = "Student",
                action = "Overview",
                Id = Id
            });
        }

        public ActionResult Overview(string Id)
        {
            CourseInfoHelper helper = new CourseInfoHelper();
            CourseOverview course = helper.GetCourseOverview(Id);
            if (course == null)
                return RedirectToAction("Index");
            return View(course);
        }

        public ActionResult Homework(string Id)
        {
            CourseInfoHelper helper = new CourseInfoHelper();
            HomeworkVM vm = helper.GetHomeworkData(Id);
            if (vm == null)
                return RedirectToAction("Index");
            return View(vm);
        }

        public ActionResult HomeworkDetails(string CourseId, string HomeworkId)
        {
            CourseInfoHelper helper = new CourseInfoHelper();
            HomeworkFullItem form = helper.GetHomeworkFullItemWithMyAnswer(CourseId, HomeworkId, User.Identity.Name);

            if (form == null)
                return RedirectToRoute(new
                {
                    controller = "Student",
                    action = "CompleteHomework",
                    CourseId = CourseId,
                    HomeworkId = HomeworkId
                });
            return View(form);
        }

        public ActionResult CompleteHomework(string CourseId, string HomeworkId)
        {
            CourseInfoHelper helper = new CourseInfoHelper();
            HomeworkFullItem form = helper.GetHomeworkFullItem(CourseId, HomeworkId);
            if (form == null)
                return RedirectToAction("/Index");
            return View(form);
        }

        public ActionResult DoCompleteHomework(HomeworkFullItem form)
        {
            CourseInfoHelper helper = new CourseInfoHelper();
            helper.CompleteHomework(form, Request["AnswerList"], User.Identity.Name);
            return RedirectToRoute(new
            {
                controller = "Student",
                action = "HomeworkDetails",
                CourseId = form.Id,
                HomeworkId = form.HomeworkId
            });
        }
    }
}